package top.ranzhang.controller.admin;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.ranzhang.bean.dto.CoashDTO;
import top.ranzhang.bean.entity.Coash;
import top.ranzhang.bean.vo.CoashUsernameIdVO;
import top.ranzhang.bean.vo.CoashVO;
import top.ranzhang.common.result.Result;
import top.ranzhang.service.ICoashService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 教练管理
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/coash")
public class CoashController {

    private final ICoashService coashService;

    /**
     * 获取所有老师
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        log.info("获取所有老师");
        List<Coash> coashList = coashService.list();
        return Result.success(coashList);
    }

    /**
     * 删除老师
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除老师：{}", id);
        coashService.removeById(id);
        return Result.success();
    }

    /**
     * 新增老师
     * @param coashDTO
     * @return
     */
    @PostMapping("/save")
    public Result save(@Validated @RequestBody CoashDTO coashDTO) {
        log.info("新增老师：{}", coashDTO);
        coashService.add(coashDTO);
        return Result.success();
    }
    /**
     * 更新老师
     */
    @PutMapping("/update")
    public Result update(@Validated @RequestBody CoashDTO coashDTO) {
        log.info("更新老师：{}", coashDTO);
        Coash coash = new Coash();
        BeanUtils.copyProperties(coashDTO,coash);
        coash.setUpdateTime(LocalDateTime.now());
        coashService.updateById(coash);
        return Result.success();
    }

    /**
     * 通过id获取老师信息
     */
    @GetMapping("/getById")
    public Result<CoashDTO> getById(Integer id) {
        log.info("通过id获取老师信息：{}", id);
        CoashDTO coashDTO = new CoashDTO();
        Coash Coash = coashService.getById(id);
        BeanUtils.copyProperties(Coash,coashDTO);
        return Result.success(coashDTO);
    }

    /**
     * 获取所有老师姓名
     * @return
     */
    @GetMapping("/getAllUsername")
    public Result getAllUsername() {
        log.info("获取所有老师姓名");
        List<CoashUsernameIdVO> list = coashService.getAllUsername();
        return Result.success(list);
    }
}
